#include <iostream>

class C {
public:
    C() { std::cout << "C::C()" << std::endl; }
    ~C() { std::cout << "C::~C()" << std::endl; }
};

class Test {
public:
    Test() { std::cout << "Test::Test()" << std::endl; }
    ~Test() { std::cout << "Test::~Test()" << std::endl; }

public:
    void* operator new(std::size_t size)
    {
        std::cout << "Test::operator new(std::size_t), size: " << size << std::endl;
        return malloc(size);
    }

    void operator delete(void* p)
    {
        std::cout << "Test::operator delete(void*)" << std::endl;
        free(p);
    }

    void* operator new(std::size_t size, void* p)
    {
        std::cout << "Test::placement new, size: " << size << std::endl;
        return p;
    }
};

int main() {
    void* p = malloc(sizeof(int) * 10);
    free(p);

    void* pc = operator new(sizeof(C));
    C* p_c = static_cast<C*>(pc);
    //p_c->C::C();  //无法直接调用, 仅仅做演示

    p_c->~C();
    operator delete(p_c);

    void* start = operator new(sizeof(C));
    C* c = new(start)C();
    /*
    * void* mem = operator new(sizeof(C), start);
    * C* p_c1 = static_cast<C*>(pc);
    * p_c1->C::C();
    */

    c->~C();
    operator delete(start);


    Test* t = new Test();
    delete t;

    void* startT1 = operator new(sizeof(Test));
    Test* t1 = new(startT1)Test();
    t1->~Test();
    operator delete(startT1);

    return 0;
}